<template>
<div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
      <el-breadcrumb-item>所有用户</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
     <!-- <el-row :gutter="12">
          <el-col :span="8">
        
          <el-input placeholder="请输入用户名" v-model="query.name" clearable>
            <el-button slot="append" icon="el-icon-search" @click="getStudents"></el-button>
          </el-input>
        </el-col> 
      </el-row> -->

      <!-- 列表区域 -->
    <el-table
    :data="userList"
    style="width: 100%"
    :default-sort = "sort"
    >
    <el-table-column  prop="ID"   label="ID"   sortable   width="80"> </el-table-column>
    <el-table-column  prop="CreatedAt"   label="创建日期"   sortable   width="180">
    <template slot-scope="scope">
        {{scope.row.CreatedAt|dataFormat}}
    </template>
     </el-table-column>
    <el-table-column  prop="userName"   label="用户名"   sortable   width="180"> </el-table-column>
    <el-table-column  prop="nickName"   label="用户类别"   sortable   width="180"> </el-table-column>
    <el-table-column  prop="headerImg"   label="用户头像"   sortable   width="180"> 
        <template slot-scope="scope">
            <el-avatar class="product-menu-icon" :src="scope.row.headerImg"></el-avatar>
        </template>
    </el-table-column>
    <el-table-column  prop="authorityId"   label="用户权限"   sortable   width="180">
    <template slot-scope="scope">
        <el-select v-model="scope.row.authorityId" :placeholder="scope.row.authorityId" @change="changeStu(scope.row)">
                <el-option v-for="(item,key) in userTypeMap"
                            :key="key"
                            :value="item.value"
                            :label="item.label">
                </el-option>
        </el-select>
    </template>
    </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
             <!-- 删除按钮 -->
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="delStu(scope.row)">永久删除用户</el-button>
          </template>
        </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="query.page"
      :page-sizes="[ 5, 8, 15]"
      :page-size="query.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>

</div>
</template>
<script>
// 
export default {
  name: "user",
  data() {
    return {
        userList:[],
        total:0,
        query:{
            page:1,
            pageSize:5,
            name:null,
        },
        sort:{
        prop: 'ID',
        order: 'ascending'
        },
        userform:{
         authorityId:null,
         id:null,
        },
       userTypeMap:{},
    }
  },
  created(){
      this.fullUserMap()
  },
  mounted() {
    this.getUsers();
  },
  methods: {
      //监听size 改变的事件 
    handleSizeChange(newSize){
      console.log(newSize)
      this.query.pageSize=newSize
      this.getUsers()
    },
    //监听页码指改变的事件
    handleCurrentChange(newPage){
      console.log(newPage)
      this.query.page=newPage
      this.getUsers()
    },
    async getUsers() {
      const { data: res } = await this.$http.get("/stu/users",{params:this.query} );
      console.log(res)
      if (res.code != 0) return this.$message.error("获取用户列表失败");
      this.userList = res.data.data;
      this.total=res.data.total
    },
    async delStu(row){
      var query={
        id:row.ID
      }
        const { data: res } = await this.$http.post("/stu/killUser",query);
      if (res.code !== 0) return this.$message.error("删除用户失败");
      this.getUsers()
      return this.$message.success("删除用户成功")
    },
    async changeStu(row){
        this.userform.authorityId=row.authorityId
        this.userform.id=row.ID
        const { data: res } = await this.$http.put("/stu/user/setAuthority",this.userform);
        console.log(res)
        if (res.code !== 0) return this.$message.error("设置权限失败");
        this.$message.success("设置权限成功")
        
    },
    async fullUserMap(){
    const { data: res } = await this.$http.get("/stu/getAllAuthority");
      if (res.code != 0) return this.$message.error("获取用户列表失败");
      this.userTypeMap =res.data.map((item)=>{
            return {
                label:item.authorityName,
                value:item.authorityId,
            }
      })
    }
}
}
</script>
<style lang="less" scoped>
.el-breadcrumb {
  margin-bottom: 15px;
}
.el-card {
  box-shadow: 0 1px 1px;
}
.card-bottom{
    margin-bottom: 10px;
    padding-left: 40%;
}
.el-table{
  margin-top:10px;
}
</style>
